//
//  SongListView.m
//  NewMusic
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SongListView.h"

@interface SongListView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation SongListView
- (void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 300) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:38 / 255.0 green:58 / 255.0 blue:59 / 255.0 alpha:1.0];
    [self addSubview:self.tableView];
    [_tableView release];
    self.dataArray = [NSMutableArray array];
    self.backgroundColor = [UIColor redColor];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdent = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdent] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
    }
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}
- (void)setDataArrayWith:(NSMutableArray *)arr
{
    self.dataArray = [NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *head = (UITableViewHeaderFooterView *)view;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(170, 0, 35, 20)];
    imageView.image = [UIImage imageNamed:@"icon-xiala"];
    [head addSubview:imageView];
    [imageView release];
    view.tintColor =  [UIColor colorWithRed:38 / 255.0 green:58 / 255.0 blue:59 / 255.0 alpha:1.0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [view addGestureRecognizer:tap];
    [tap release];
}
- (void)tapAction
{
    [self.delegate returnMusicList:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate returnMusicList:[NSString stringWithFormat:@"%ld", indexPath.row]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
@end
